﻿(function (wtm) {
    wtm.dialog_Render = function (para) {
        var $win = $(window);
        var $dialog = $('#' + para.id);

        //设置最大高度，防止溢出屏幕
        $dialog.css('max-height', $win.height() - 100 + 'px');

        var layero = wtm.dialog_getCurrentIndex();

        if (layero != false) {
            var option = wtm.dialog_calc(para);

            //渲染
            layui.layer.style(layero.index, {
                width: option.width + 'px',
                height: option.height + 'px'
            });

            //设置标题
            if (para.title.length > 0) {
                layui.layer.title(para.title, layero.index);
            }

            //响应时,添加监听事件
            if (para.isResponsive) {
                $(window).resize(function (event) {
                    var opt = wtm.dialog_calc(para);

                    //渲染
                    layui.layer.style(layero.index, {
                        width: opt.width + 'px',
                        height: opt.height + 'px'
                    });
                });
            }
        }
    };

    wtm.dialog_calc = function (para) {
        var option = {
            width: null,
            height: null
        };
        var $win = $(window);

        //负值时
        if (para.width < 0) {
            option.width = $win.width() + para.width;
        }
        if (para.height < 0) {
            option.height = $win.height() + para.height;
        }

        //百分比时
        if (para.isPercent) {
            var width = Math.abs(para.width);
            var height = Math.abs(para.height);

            if (width > 100) { width = 100; }
            if (height > 100) { height = 100; }

            if (width > 0) {
                option.width = $win.width() * width / 100;
            }
            if (height > 0) {
                option.height = $win.height() * height / 100;
            }
        }

        //移动环境下，将宽度设置为最大
        if (wtm.mobile) {
            option.width = $win.width() - 20;
        }
        return option;
    };

    wtm.dialog_getCurrentIndex = function () {
        var wid = ff.GetCookie("windowids");
        if (wid !== null && wid !== '') {
            var wids = wid.split(",");
            var windowid = wids.pop();
            var layro = $('#' + windowid).parent('.layui-layer');
            return {
                $elem: layro,
                index: layro.attr("times")
            };
        }
        return false;
    };
})(wtm);